//
//  CarActivityCollectionViewController.m
//  我的小汽车
//
//  Created by 邹壮壮29 on 15-1-13.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "CarActivityCollectionViewController.h"
#import "CarActivityBase.h"
#import "ActivityModel.h"
typedef NS_ENUM(NSInteger, editorState) {
    editorNo,
    editorYes
};
@interface CarActivityCollectionViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (assign, nonatomic)editorState editorState;
@end

@implementation CarActivityCollectionViewController
-(void)dealloc{
    [_carActivityArray release];
    [_tableView release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.carActivityArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSArray *array = [CarActivityBase selectAllCarActivity];
    [self.carActivityArray addObjectsFromArray:array];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
   self.rightButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/5*4, 0, self.view.frame.size.width/5, 44)];
    [_rightButton setTitle:@"编辑" forState:UIControlStateNormal];
    _rightButton.backgroundColor = [UIColor grayColor];
    [_rightButton addTarget:self action:@selector(editor) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_carActivityArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    ActivityModel *model = [_carActivityArray objectAtIndex:indexPath.row];
    cell.textLabel.text = model.event_title;
    return cell;
}
-(void)edit{
    [_tableView setEditing:YES animated:YES];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete){
        ActivityModel * model = [_carActivityArray objectAtIndex:indexPath.row];
        
        [CarActivityBase deleteCarActivityWithModel:model];
        [self.carActivityArray removeObjectAtIndex:indexPath.row];
        [_tableView reloadData];
    }
}
//改变
-(void)editor{
    if (self.editorState == editorNo) {
        [_rightButton setTitle:@"编辑" forState:UIControlStateNormal];
        [_tableView setEditing:NO animated:YES];
        self.editorState = editorYes;
    }
    else if (self.editorState == editorYes) {
        [_tableView setEditing:YES animated:YES];
        [_rightButton setTitle:@"完成" forState:UIControlStateNormal];
        self.editorState = editorNo;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
